#!/usr/bin/env python
import sys, hashlib, math

print """
Trefoil 1.0 - Project Trefoil
Copyright 2009 Juti Noppornpitak <shiroyuki@gmail.com> and Panote Siriaraya <spanote@gmail.com>
Dual-licensed under GNU Lesser General Public License 2.1 and MIT License

"""

def getInputWithExpectation(promptMessage, expectedResponses = None):
    origPM = promptMessage
    promptMessage = "\n{0}\n[Expecting: {1}]\n>> ".format(promptMessage, ', '.join(expectedResponses))
    response = raw_input(promptMessage)
    response = response.strip()
    if expectedResponses is not None and response not in expectedResponses:
        print "<< Your response is unknown"
        return getInputWithExpectation(origPM, expectedResponses)
    return response

def getInputWithDefaultValue(promptMessage, defaultResponse = None):
    origPM = promptMessage
    if defaultResponse is None:
        promptMessage = "\n{0}\n>> ".format(promptMessage)
    else:
        promptMessage = "\n{0}\n[Default: {1}]\n>> ".format(promptMessage, defaultResponse)
    response = raw_input(promptMessage)
    response = response.strip()
    if defaultResponse is None and response == '':
        print "<< Must enter some value"
        return getInputWithDefaultValue(origPM, defaultResponse)
    elif defaultResponse is not None and response == '':
        print "<< Use the default value"
        response = defaultResponse
    return response

isProduction = False
doAgree = getInputWithExpectation(
    "By running this script, you are assumed that you agree the conditions in either the GNU LGPL 2 or MIT license. Do you want to continue?",
    ['Y','y','N','n']
)

if doAgree in ['N', 'n']:
    print "Thanks for trying! :)"
    print
    sys.exit()

s = {
    'here':         '%(here)s',
    'asctime':      '%(asctime)s',
    'levelname':    '%(levelname)-5.5s',
    'name':         '%(name)s',
    'message':      '%(message)s'
}

s['errorMailTo'] = getInputWithDefaultValue(
    "Assuming that this server has mailing service running, when there is error, who do you want the system to send e-mail to?"
)

s['errorMailFrom'] = getInputWithDefaultValue(
    "Then, who is the sender (email)?"
)

s['userDir'] = getInputWithDefaultValue(
    "Where is the base for the user directory? For example, /var/www/html/trefoil/user_directory",
    "%(here)s/data/user_directory/"
)

s['homeDir'] = getInputWithDefaultValue(
    "What is the base URL? For example, /var/www/html/trefoil/users",
    "%(here)s/data/u/"
)

s['serviceURL'] = getInputWithDefaultValue(
    "What is the service URL, where the web interface is? For example, http://s.trefoil.shiroyuki.com."
)

s['baseURL'] = getInputWithDefaultValue(
    "What is the base URL, where the user homepage is? For example, http://u.trefoil.shiroyuki.com."
)

s['adminUsername'] = getInputWithDefaultValue(
    "As an administrator, you can only use one special username to administer the whole system. Nevertheless, this username will be forbidden from using the service. What is the username of the administrator?"
)

s['adminPassword'] = getInputWithDefaultValue(
    "What is the password?"
)

s['adminEncodedPassword'] = "%s%s" % (hashlib.sha1(s['adminPassword']).hexdigest(), hashlib.md5(s['adminPassword']).hexdigest())

s['quotaStorage'] = int(getInputWithDefaultValue(
    "What is the size of the user storage in MB?",
    '100'
)) * math.pow(2, 20)

s['uploadLimit'] = int(getInputWithDefaultValue(
    "What is the upload limit in MB?",
    '20'
)) * math.pow(2, 20)

s['database'] = getInputWithDefaultValue(
    "Please tell the URL of the database. The URL is a string in the form dialect://user:password@host/dbname[?key=value..], where dialect is a name such as mysql, oracle, postgres, etc. Alternatively, the URL can be an instance of URL.",
    "mysql://root@localhost/trefoil"
)

import yotsuba

settingsTemplate = yotsuba.fs.read('production_orig.ini')
yotsuba.fs.write('production.ini', settingsTemplate % s)
